import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/components/categoryLeftMenu.dart';
import 'package:flutter_shop/components/categoryRightPanel.dart';
import 'package:flutter_shop/helpers/Api.dart';
import 'package:flutter_shop/models/categroyModel.dart';
import 'package:flutter_shop/provider/category.dart';
import 'package:provider/provider.dart';

class CategoryPage extends StatefulWidget {
  @override
  _CategoryPageState createState() {
    return _CategoryPageState();
  }
}

class _CategoryPageState extends State<CategoryPage> {
  List<CategoryBigModel> list = [];
  CategoryProvider category = CategoryProvider();
  // ChangeNotifierProvider.value(value: categroyProvider)

  @override
  void initState() {
    this._getCategory();
    super.initState();
  }

  Future _getCategory() async {
    var res = await Api.getCategory();
    if (res.code == "0") {
      CategoryBigListModel category = CategoryBigListModel.fromJson(res.data);
      this.list = category.data;
      this.category.setLeftMenuList(category.data);
      return category;
    }
    print("服务器错误code: ${res.code}, msg: ${res.message}");
    return null;
    // NOTE: 使用 provide change state
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return Scaffold(
        appBar: AppBar(title: Text("商品分类"), centerTitle: true),
        body: ChangeNotifierProvider<CategoryProvider>.value(
          value: this.category,
          child: Container(
            child: Row(
              children: <Widget>[
                CategoryLeftMenu(),
                CategoryRightPenel(),
              ],
            ),
          ),
        ));
  }
}
